package com.huqu.beans.video;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;

import com.huqu.beans.sessionWeb.SessionWeb;
import com.huqu.dao.N500Dao;
import com.huqu.entities.N500;
import com.huqu.utils.ApplicationConstant;
import com.huqu.utils.ApplicationHelper;

/**
 * @author CONGQUYNH
 */
@ManagedBean
@ViewScoped
public class VideoChiTietBean implements Serializable {
	private static final long serialVersionUID = -3265442710467500468L;

	@ManagedProperty("#{sessionWeb}")
	private SessionWeb sessionWeb;
	
	private String vLang="";
	private String md5video="";
	private boolean exitsVideo=true;//kiểm tra xem video có tồn tại hay không ?
	private String title="";// Làm tiêu đề của page
	List<N500> listN500INFO= new ArrayList<N500>();
	private N500 n500Info= new N500();
	
	
	@PostConstruct
	public void init(){
		try {
			System.out.println("====== VideoBean =======");
			// kiểm tra ngôn ngữ
			if(ApplicationHelper.getRequest().getParameter(ApplicationConstant.PRETTY_PARA_LANG)!=null){
				vLang=ApplicationHelper.getRequest().getParameter(ApplicationConstant.PRETTY_PARA_LANG).toString().toUpperCase();
				
				if(vLang.trim().toUpperCase().equals("VN")){
					if(sessionWeb.locale.equals(Locale.US)){
						sessionWeb.setLocale(new Locale("vi", "VN"));
						FacesContext.getCurrentInstance()
		    			.getViewRoot().setLocale(new Locale("vi", "VN"));
						RequestContext.getCurrentInstance().execute( "location.reload();");
					}
				}else if(vLang.trim().toUpperCase().equals("EN")){
					if(sessionWeb.locale.equals(new Locale("vi", "VN"))){
						sessionWeb.setLocale(Locale.US);
						FacesContext.getCurrentInstance()
		    			.getViewRoot().setLocale(Locale.US);
						RequestContext.getCurrentInstance().execute( "location.reload();");
					}
				}
			}else{
				System.out.println("---language khong xac dinh ");
			}
			
			if(ApplicationHelper.getRequest().getParameter(ApplicationConstant.PRETTY_PARA_IDVIDEO)!=null){
				md5video=ApplicationHelper.getRequest().getParameter(ApplicationConstant.PRETTY_PARA_IDVIDEO);
				System.out.println("---idvideo= "+md5video);
				
				//kiểm tra xem tin tức có tồn tại hay không 
				getListOfTabN500INFO();
				if(listN500INFO.size()==0){
					if(vLang.toLowerCase().equals("VN")){
						title="The video doesn't exits.";
					}else{
						title="Video không tồn tại.";
					}
					exitsVideo=false;
				}else{
					title=listN500INFO.get(0).getTenvideo();
					n500Info=listN500INFO.get(0);
					exitsVideo=true;
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// load thông tin chi tiết của video
	public void getListOfTabN500INFO(){
		try {
			N500Dao n500Dao = (N500Dao) ApplicationHelper.findBean(ApplicationConstant.REPOSITORY_NAME_N500);
			System.out.println("---ListOfTabN500INFO( "+md5video+" , "+ vLang+" )");
			listN500INFO=n500Dao.ListOfTabN500INFO(md5video, vLang);
			System.out.println("ListN500INFO.size()= "+listN500INFO.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public SessionWeb getSessionWeb() {
		return sessionWeb;
	}

	public void setSessionWeb(SessionWeb sessionWeb) {
		this.sessionWeb = sessionWeb;
	}

	public String getvLang() {
		return vLang;
	}

	public void setvLang(String vLang) {
		this.vLang = vLang;
	}

	public String getMd5video() {
		return md5video;
	}

	public void setMd5video(String md5video) {
		this.md5video = md5video;
	}

	public boolean isExitsVideo() {
		return exitsVideo;
	}

	public void setExitsVideo(boolean exitsVideo) {
		this.exitsVideo = exitsVideo;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public List<N500> getListN500INFO() {
		return listN500INFO;
	}

	public void setListN500INFO(List<N500> listN500INFO) {
		this.listN500INFO = listN500INFO;
	}

	public N500 getN500Info() {
		return n500Info;
	}

	public void setN500Info(N500 n500Info) {
		this.n500Info = n500Info;
	}
	
}
